<%@ page pageEncoding="UTF-8" %>
<%@ include file="/common/validate.jsp" %>
<%@ include file="../function.jsp" %>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>科室信息</title>
    <link href="../../images/favicon.ico?cache=<%=Math.random()%>" rel="shortcut icon" type="image/x-icon">
    <link href="https://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/editor/summernote.css" rel="stylesheet">
    <link rel="stylesheet" href="/css/jquery.selectlist.css?cache=<%=Math.random()%>">
    <link rel="stylesheet" href="/css/public.css?cache=<%=Math.random()%>">
    <link rel="stylesheet" href="/css/messages_dep_mes.css?cache=<%=Math.random()%>">
    <link rel="stylesheet" href="/css/sweetalert.css?cache=<%=Math.random()%>" >
    <%--编辑器--%>
    <script src="../../js/jquery-1.9.1.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" />
    <script src="/css/editor/summernote.js"></script>

</head>
<body>
<%@ include file="../../header.jsp" %>
<!--内容-->
<div class="containerone">
    <%@ include file="../../left_menu.jsp" %>
    <!--右边内容-->
    <div class="right_cont">
        <div id="app">
            <dept></dept>
        </div>
    </div>
</div>
<script type="x-template" id="dept">
    <div class="message_right_cont_box">
        <div class="message_right_cont_container">
            <div class="department_content">

                <ul class="first_subject">
                    <template v-for="first_dept in first_depts">
                        <li class="subject to_second_subject" @click="click_first_dept(first_dept.deptCode)"
                            v-on:mouseenter="show_rubbish($event)" v-on:mouseleave="hide_rubbish($event)">
                            <p v-html="first_dept.deptName"></p>
                            <%
                                if (checkAuth("删除科室", input)) {
                            %>
                            <span class="delete_dep" @click="delete_dept(first_dept.deptCode)">
        <img v-show="false" src="/images/rubbish.png"></span>
                            <%
                                }
                            %>
                        </li>
                    </template>
                    <%
                        if (checkAuth("添加科室", input)) {
                    %>
                    <li class="add_first_subject" @click="add_dept($event)">+添加一级科室</li>
                    <%
                        }
                    %>
                </ul>
                <ul class="second_subject" >
                    <template v-for="second_dept in second_depts ">
                        <li class="subject" @click="click_second_dept(second_dept.deptCode)" v-on:mouseenter="show_rubbish($event)"
                            v-on:mouseleave="hide_rubbish($event)">
                            <p v-html="second_dept.deptName" ></p>
                            <%
                                if (checkAuth("删除科室", input)) {
                            %>
                            <span class="delete_dep" @click="delete_dept(second_dept.deptCode)">
        <img v-show="false" src="/images/rubbish.png"></span>
                            <%
                                }
                            %>
                        </li>
                    </template>
                    <%
                        if (checkAuth("添加科室", input)) {
                    %>
                    <li class="add_second_subject" @click="add_dept($event)">+添加二级科室</li>
                    <%
                        }
                    %>
                </ul>
                <!--科室介绍-->
                <div class="edit_department_con" v-if="dept_intro">
                    <ul class="subject_detail">
        <span class="see_department_image">
        <span class="see_department_image_in">
        <img :src="dept_detail.img" alt="" v-if="dept_detail.img != '' && dept_detail.img != undefined">
        <img src="/images/no_image.png" v-else>
        </span>
        </span>
                        <li class="edit_department_li">科室名称:<span v-html="dept_detail.dept_name"></span></li>
                        <li class="edit_department_li ">科室编号:<span v-html="dept_detail.dept_id"></span></li>
                        <li class="edit_department_li">科室类型:<span v-html="dept_detail.type"></span></li>
                        <li class="edit_department_li">科室地址:<span v-html="dept_detail.json.address"></span></li>
                        <li class="edit_department_li">咨询电话:<span v-html="dept_detail.json.phone"></span></li>
                        <li>
                            <a><button class="ui_btn" @click="link_news(dept_detail.dept_id,dept_detail.dept_name)">科室新闻管理</button></a>
                            <button class="ui_btn link_detpts" data-toggle="modal" data-target="#myModal"
                                    @click="link_dept(dept_detail.id)">关联挂号科室</button>
                        </li>
                        <div>
                            <span>科室介绍</span>
                            <p v-html="decode(dept_detail.content)"></p>
                            <%
                                if (checkAuth("编辑科室", input)) {
                            %>
                            <button id="edit_department_message" class="edit_department_message" @click="edit_dept()">编辑科室信息</button>
                            <%
                                }
                            %>
                        </div>
                    </ul>
                </div>
                <%--</div>--%>
                <%--编辑科室--%>
                <div class="edit_department_con" v-if="dept_edit">
                    <div class="edit_department">
                        <li class="edit_department_image">
                            <span ><input type="file" type="text"></span>
                            <div class="edit_department_image_in">
                                <img :src="dept_detail.img" alt="" >

                            </div>
                            <span class="up_tip">点击图片及可上传</span>
                        </li>
                        <div class="clearfix"></div>
                        <li class="edit_department_li">科室名称：<span><input v-model="dept_detail.dept_name" class="one_two_type
        deptName_tip" name="deptName" type="text" ></span></li>
                        <li class="edit_department_li">科室编号：<span><input v-model="dept_detail.dept_id" class="deptCode_tip"
                                                                         readonly="true" name="deptCode" type="text" placeholder="请输入科室编号"></span></li>
                        <li class="edit_department_li">科室地址：<span><input v-model="dept_detail.json.address" class="deptAddress_tip"
                                                                         name="address" type="text" placeholder="如:门诊部三楼北区"></span></li>
                        <li class="edit_department_li">咨询电话：<span><input v-model="dept_detail.json.phone" class="deptPhone_tip"
                                                                         name="phone" type="text" placeholder="填写科室或者电话,如:XXX"></span></li>
                        <li class="edit_department_li">科室介绍：<span class="intro_limit">(限输入2000个字)</span></li>
                        <div id="editor"></div>
                        <div class="message_save_giveup">
                            <button class="message_giveup giveup_add_subject" @click="give_up()">放弃</button>
                            <button class="message_save save_add_first_subject" @click="edit_submit()">保存</button>
                        </div>
                    </div>
                </div>
                <%--添加科室--%>
                <div class="edit_department_con" v-if="dept_add">
                    <div class="edit_department">
                        <li class="edit_department_image">
                            <span ><input type="file" type="text"></span>
                            <div class="edit_department_image_in">
                                <%--<img src="../images/dep_upload_image.jpg" alt="" >--%>
                            </div>
                        </li>
                        <div class="clearfix"></div>
                        <li class="edit_department_li">科室名称：<span><input v-model="add_deptName" class="one_two_type deptName_tip"
                                                                         name="deptName" type="text" ></span></li>
                        <li class="edit_department_li">科室编号：<span><input v-model="add_deptCode" type="text" class="deptCode_tip"
                                                                         placeholder="请输入科室编号"></span></li>
                        <li class="edit_department_li">科室地址：<span><input v-model="add_address" name="address" class="deptAddress_tip"
                                                                         type="text" placeholder="如:门诊部三楼北区"></span></li>
                        <li class="edit_department_li">咨询电话：<span><input v-model="add_phone" name="phone" class="deptPhone_tip"
                                                                         type="text" placeholder="填写科室或者电话,如:XXX"></span></li>
                        <li class="edit_department_li">科室介绍：<span class="intro_limit">(限输入2000个字)</span></li>
                        <div id="editortwo"></div>
                        <div class="message_save_giveup">
                            <button class="message_giveup giveup_add_subject" @click="give_up()">放弃</button>
                            <button class="message_save save_add_first_subject" @click="submit()">保存</button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
        </div>
        <%--模太框--%>
        <div class="modal_box">
            <%--模态框--%>
            <div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                                    class="sr-only"></span></button>
                            <h4 class="modal-title" id="myModalLabel">关联挂号科室</h4>
                        </div>
                        <div class="modal-body">
                            <div class="modal_top">
                                <template v-for="link in all_links.links">
        <span>
        <p v-html="link.deptName"></p>
            <%
                if (checkAuth("关联挂号科室", input)) {
            %>
        <div class="modal_top_delete" @click="delete_choose_link(link.deptCode)"></div>
            <%
                }
            %>
        </span>
                                </template>
                            </div>
                            <div class="modal_middle_search">
                                <input type="text" class="model_search_input" @change="search($event)" id="search" placeholder="请输入要查询的科室名称"
                                ><span class="model_search_image"></span>
                            </div>
                            <div class="clearfix"></div>
                            <div class="modal_title"><span class="model_dept_id">科室编号</span><span class="model_dept_name">科室名称</span></div>
                            <div class="modal_button_checkbox">
                                <template v-for="all_link in all_links.depts">
                                    <li class="dept_name" :data-name=all_link.dept_name>
                                        <span class="model_dept_id"><p class="inline_p" v-html="all_link.dept_id"></p></span>
                                        <span class="model_dept_name" v-html="all_link.dept_name" ></span>
                                        <%
                                            if (checkAuth("关联挂号科室", input)) {
                                        %>
                                        <input name="checkbox" v-model="all_link.is_linked" @change="choose_link(all_link)" type="checkbox">
                                        <%
                                            }
                                        %>
                                    </li>
                                </template>
                            </div>
                            <div class="clearfix"></div>
                            <%
                                if (checkAuth("关联挂号科室", input)) {
                            %>
                            <button type="button" class="model_btn_default" data-dismiss="modal">取消</button>
                            <button type="button" class="model_btn_success" aria-hidden="true" data-dismiss="modal"
                                    @click="link_sure()">确定</button>
                            <%
                                }
                            %>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>

<script src="/js/sweetalert-dev.js?cache=<%=Math.random()%>"></script>
<script type="text/javascript">
    var dept = {
        template :"#dept",
        data: function() {
            return {
                first_depts:[],
                second_depts:[],
                dept_detail:[],
                all_links:[],
                dept_intro:true,
                dept_edit:false,
                dept_add:false,
                type:'',
                parent_id:'',
                add_parent_id:'',
                add_deptName:'',
                add_deptCode:'',
                add_address:'',
                add_phone:'',
                add_content:'',
                add_head_image:'',
            }
        },
        ready:function(){
            var self=this;
            $.ajax({
                type: 'post',
                url: "see.jsp",
                data: {
                    act:"first_depts",
                },
                timeout: 3000,
                dataType: 'json',
                success: function(res) {
                    if (res.errcode == 0) {
                        self.first_depts= res.depts;
                        self.parent_id=self.first_depts[0].deptCode;
                        $.ajax({
                            type: 'post',
                            url: "see.jsp",
                            data: {
                                act:"second_depts",
                                deptCode:self.first_depts[0].deptCode
                            },
                            timeout: 3000,
                            dataType: 'json',
                            success: function(res) {
                                if (res.errcode == 0) {
                                    self.second_depts= res.depts;
                                }else{
                                    self.second_depts=[]
                                }
                                setTimeout (show,0);
                                function show()
                                {
                                    $('.first_subject .subject').eq(0).addClass('sub_bg_color');
                                    $('body').on('click','.first_subject .subject',
                                        function(){
                                            $(this).addClass('sub_bg_color').siblings().removeClass('sub_bg_color');})
                                    $('body').on('click','.second_subject .subject',
                                        function(){
                                            $(this).addClass('sub_bg_color').siblings().removeClass('sub_bg_color');})
                                    $('.first_subject li span img,.second_subject li span img').hide();
                                }

                            },
                            error: function(error) {

                            }
                        });
                        $.ajax({
                            type: 'post',
                            url: "see.jsp",
                            data: {
                                act:"show_dept",
                                deptCode:self.first_depts[0].deptCode
                            },
                            timeout: 3000,
                            dataType: 'json',
                            success: function(res) {
                                if (res.errcode == 0) {
                                    self.dept_detail= res.dept;
                                    self.parent_id=self.dept_detail.dept_id;
                                    if(self.dept_detail.content==undefined||self.dept_detail.content==''){
                                        self.dept_detail.content="暂无"
                                    }
                                }else{
                                    self.dept_detail=[]
                                }

                            },
                            error: function(error) {

                            }
                        });
                    }else{
                        self.first_depts=[]
                    }
                },
                error: function(error) {

                }
            });

        },
        methods:{
            show_rubbish:function(event){
                $(event.target).find('img').show()
            },
            hide_rubbish:function(event){
                $(event.target).find('img').hide()
            },
            <%--通过点击一级科室获取二级科室以及一级科室的信息--%>
            click_first_dept:function(deptCode){
                var self = this;
                self.dept_intro=true;
                self.dept_edit=false;
                self.dept_add=false;
                self.parent_id=deptCode;
                $.ajax({
                    type: 'post',
                    url: "see.jsp",
                    data: {
                        act:"second_depts",
                        deptCode:deptCode
                    },
                    timeout: 3000,
                    dataType: 'json',
                    success: function(res) {
                        if (res.errcode == 0) {
                            if (res.errcode == 0) {
                                self.second_depts= res.depts;
                            }else{
                                self.second_depts=[]
                            }
                            setTimeout (show,0);
                            function show()
                            {
                                $('body').on('click','.first_subject .subject',
                                    function(){
                                        $(this).addClass('sub_bg_color').siblings().removeClass('sub_bg_color');})
                                $('body').on('click','.second_subject .subject',
                                    function(){
                                        $(this).addClass('sub_bg_color').siblings().removeClass('sub_bg_color');})
                                $('.first_subject li span img,.second_subject li span img').hide();
                            }
                        }
                    },
                    error: function(error) {
                    }
                });
                $.ajax({
                    type: 'post',
                    url: "see.jsp",
                    data: {
                        act:"show_dept",
                        deptCode:deptCode
                    },
                    timeout: 3000,
                    dataType: 'json',
                    success: function(res) {
                        if (res.errcode == 0) {
                            self.dept_detail= res.dept;
                            self.parent_id=self.dept_detail.dept_id;
                            if(self.dept_detail.content==undefined||self.dept_detail.content==''){
                                self.dept_detail.content="暂无"
                            }
                        }else{
                            self.dept_detail=[]
                        }
                    },
                    error: function(error) {

                    }
                });

            },
            <%--通过点击二级科室获取二级科室的信息--%>
            click_second_dept:function(deptCode){
                var self = this;
                self.dept_intro=true;
                self.dept_edit=false;
                self.dept_add=false;
                $.ajax({
                    type: 'post',
                    url: "see.jsp",
                    data: {
                        act:"show_dept",
                        deptCode:deptCode
                    },
                    timeout: 3000,
                    dataType: 'json',
                    success: function(res) {
                        if (res.errcode == 0) {
                            self.dept_detail= res.dept;
                            if(self.dept_detail.content==undefined||self.dept_detail.content==''){
                                self.dept_detail.content="暂无"
                            }
                        }else{
                            self.dept_detail=[]
                        }
                    },
                    error: function(error) {

                    }
                });
            },
            add_dept:function(event){
                var self = this;
                function editor(){
                    $(document).ready(function() {
                        $('#editortwo').summernote({
                            height:200,
                            lang:'zh-CN',
                            callbacks: {
                                onImageUpload: function(files) {
                                    // upload image to server and create imgNode...
                                    var img = localImg(files[0]);
                                }
                            }
                        });
                    });
                    function localImg(file) {
                        var xmlhttprequest;
                        var form = new FormData();
                        form.append("file", file);
                        if (window.XMLHttpRequest) {
                            xmlhttprequest = new XMLHttpRequest();
                            if (xmlhttprequest.overrideMimeType) {
                                xmlhttprequest.overrideMimeType("text/xml");
                            }
                        }
                        xmlhttprequest.open("POST", "http://cloud.eeesys.com/pu/upload.php", true);
                        xmlhttprequest.onreadystatechange = callback.bind(this);
                        xmlhttprequest.send(form);
                        function callback() {
                            if (xmlhttprequest.readyState == 4 && xmlhttprequest.status == 200) {
                                var res = JSON.parse(xmlhttprequest.responseText);
                                if (res.errcode == 0) {
                                    $('#editortwo').summernote('insertImage', res.img);
                                } else {
                                    swal({
                                        txt: decodeURI(res.errmsg),
                                        title: "",
                                    });
                                }
                            }
                        }
                    }
                    var img_url="";
                    $('.edit_department_image span input').change(function(e){
                        <%--图片预览--%>
                        UpladFile(e.target);
                        var file=e.target.files[0]
                        var img=new Image();
                        img.src=URL.createObjectURL(file);
                        var url=img.src;
                        var $img=$(img);
                        img.onload=function(){
                            URL.revokeObjectURL(url);
                            $('.edit_department_image_in').empty().append($img);
                        }
                    })
                    <%--上传文件--%>
                    function UpladFile(obj) {
                        var objVal = $(obj).val();
                        var name = objVal.split("\\")[2];
                        var tail = name.split(".")[1];
                        if(tail=="jpg"||tail=="png"||tail=="jpeg"){
                            var fileObj = obj.files[0]; // js 获取文件对象
                            var FileController = "http://cloud.eeesys.com/pu/upload.php"; // 接收上传文件的后台地址
                            // FormData 对象
                            var form = new FormData();
                            form.append("author", "hooyes"); // 可以增加表单数据
                            form.append("act", "file"); // 可以增加表单数据
                            form.append("file", fileObj); // 文件对象
                            // XMLHttpRequest 对象
                            var xhr = new XMLHttpRequest();
                            xhr.open("post", FileController, true);
                            xhr.onload = function (res) {
                                var rr = JSON.parse(xhr.responseText);
                                var timestamp = Date.parse(new Date()) + "";
                                img_url=rr.url
                                self.add_head_image = img_url;
                                //console.log(img_url);
                            };
                            xhr.send(form);
                            return img_url;
                        }
                        else{
                            $(obj).val('');
                            swal("只支持jpg和png的图片格式");
                            return;
                        }
                    }}
                setTimeout (editor,0);
                <%--self.dept_detail=[]--%>
                self.dept_intro=false;
                self.dept_edit=false;
                self.dept_add=true;
                self.add_deptName='';
                self.add_deptCode='';
                self.add_address='';
                self.add_phone='';
                self.add_content='';
                self.add_head_image='';
                $('.note-editable').html('');
                setTimeout (check,0);
                function check(){
                    if($(event.target).text().trim() == "+添加一级科室"){
                        self.type=1;
                        $('.one_two_type').attr('placeholder','请输入一级科室');
                        self.add_parent_id="";
                    }else{
                        self.type=2;
                        $('.one_two_type').attr('placeholder','请输入二级科室');
                        self.add_parent_id=self.parent_id;
                    }
                }
            },
            submit:function(){
                var sHTML = $('.note-editable').html();
                var self = this;
                if($('.deptName_tip').val().trim()==""){
                    swal("科室名不能为空");
                    return;
                }
                if($('.deptCode_tip').val().trim()==""){
                    swal("科室编号不能为空");
                    return;
                }
                $.ajax({
                    type: 'post',
                    url: "see.jsp",
                    data: {
                        act:"add_dept",
                        parentId:self.add_parent_id,
                        type:self.type,
                        deptName:self.add_deptName,
                        deptCode:self.add_deptCode,
                        address:self.add_address,
                        phone:self.add_phone,
                        content:encodeURI(sHTML.replace(/"/g, "'")),
                        head_image:self.add_head_image
                    },
                    timeout: 3000,
                    dataType: 'json',
                    success: function(res) {
                        if (res.errcode == 0) {
                            location.reload()
                        }else if(res.errcode == 30031){
                            swal('科室编号重复,请重新填写')
                        }
                    },
                    error: function(error) {

                    }
                });
            },
            give_up:function(){
                var self = this;
                self.dept_intro=true;
                self.dept_edit=false;
                self.dept_add=false;
            },
            edit_submit:function(){
                if($('.deptName_tip').val().trim()==""){
                    swal("科室名不能为空");
                    return;
                }
                if($('.deptCode_tip').val().trim()==""){
                    swal("科室编号不能为空");
                    return;
                }
                var sHTML = $('.note-editable').html();
                var self = this;
                $.ajax({
                    type: 'post',
                    url: "see.jsp",
                    data: {
                        act:"update_dept",
                        deptName:self.dept_detail.dept_name,
                        deptCode:self.dept_detail.dept_id,
                        address:self.dept_detail.json.address,
                        phone:self.dept_detail.json.phone,
                        content:encodeURI(sHTML.replace(/"/g, "'")),
                        head_image:self.dept_detail.img
                    },
                    timeout: 3000,
                    dataType: 'json',
                    success: function(res) {
                        if (res.errcode == 0) {
                            location.reload()
                        }else if(res.errcode == 30031){
                            swal('科室编号重复,请重新填写')
                        }
                    },
                    error: function(error) {
                    }
                });
            },
            edit_dept:function(){
                var self = this;
                function editor(){
                    $(document).ready(function() {
                        $('#editor').summernote({
                            height:200,
                            lang:'zh-CN',
                            callbacks: {
                                onImageUpload: function(files) {
                                    // upload image to server and create imgNode...
                                    var img = localImg(files[0]);
                                }
                            }
                        });
                        var sHTML = decodeURI(self.dept_detail.content);
                        $('.note-editable').html(sHTML) ;
                    });
                    function localImg(file) {
                        var xmlhttprequest;
                        var form = new FormData();
                        form.append("file", file);
                        if (window.XMLHttpRequest) {
                            xmlhttprequest = new XMLHttpRequest();
                            if (xmlhttprequest.overrideMimeType) {
                                xmlhttprequest.overrideMimeType("text/xml");
                            }
                        }
                        xmlhttprequest.open("POST", "http://cloud.eeesys.com/pu/upload.php", true);
                        xmlhttprequest.onreadystatechange = callback.bind(this);
                        xmlhttprequest.send(form);
                        function callback() {
                            if (xmlhttprequest.readyState == 4 && xmlhttprequest.status == 200) {
                                var res = JSON.parse(xmlhttprequest.responseText);
                                if (res.errcode == 0) {
                                    $('#editor').summernote('insertImage', res.img);
                                } else {
                                    swal({
                                        txt: decodeURI(res.errmsg),
                                        title: "",
                                    });
                                }
                            }
                        }
                    }
                    var img_url="";
                    $('.edit_department_image span input').change(function(e){
                        <%--图片预览--%>
                        UpladFile(e.target);
                        var file=e.target.files[0]
                        var img=new Image();
                        img.src=URL.createObjectURL(file);
                        var url=img.src;
                        var $img=$(img);
                        img.onload=function(){
                            URL.revokeObjectURL(url);
                            $('.edit_department_image_in').empty().append($img);
                        }
                    })
                    <%--上传文件--%>
                    function UpladFile(obj) {
                        var objVal = $(obj).val();
                        var name = objVal.split("\\")[2];
                        var tail = name.split(".")[1];
                        if(tail=="jpg"||tail=="png"||tail=="jpeg"){
                            var fileObj = obj.files[0]; // js 获取文件对象
                            var FileController = "http://cloud.eeesys.com/pu/upload.php"; // 接收上传文件的后台地址
                            // FormData 对象
                            var form = new FormData();
                            form.append("author", "hooyes"); // 可以增加表单数据
                            form.append("act", "file"); // 可以增加表单数据
                            form.append("file", fileObj); // 文件对象
                            // XMLHttpRequest 对象
                            var xhr = new XMLHttpRequest();
                            xhr.open("post", FileController, true);
                            xhr.onload = function (res) {
                                var rr = JSON.parse(xhr.responseText);
                                var timestamp = Date.parse(new Date()) + "";
                                img_url=rr.url
                                self.dept_detail.img = img_url;
                                //console.log(img_url);
                            };
                            xhr.send(form);
                            return img_url;
                        }
                        else{
                            $(obj).val('');
                            swal("只支持jpg和png的图片格式");
                            return;
                        }
                    }}
                setTimeout (editor,0);
                self.dept_intro=false;
                self.dept_edit=true;
                self.dept_add=false;
            },
            decode: function(i) {
                return decodeURI(i);
            },
            <%--删除科室--%>
            delete_dept:function(deptCode){
                swal({
                    title: "您确定要删除吗？",
                    type: "warning",
                    showCancelButton: true,
                    closeOnConfirm: false,
                    confirmButtonText: "是的，我要删除",
                    cancelButtonText:"取消",
                    confirmButtonColor: "#ec6c62",
                },function(){
                    $.ajax({
                        type: 'post',
                        url: "see.jsp",
                        data: {
                            act:"delete_dept",
                            deptCode:deptCode,

                        },
                        timeout: 3000,
                        dataType: 'json',
                        success: function(res) {
                            if (res.errcode == 0) {
                                self.location.reload();
                            }else{

                            }
                        },
                        error: function(error) {

                        }
                    });})
            },
            link_dept:function(dept_id){
                var self = this;
                $.ajax({
                    type: 'post',
                    url: "see.jsp",
                    data: {
                        act:"register_depts",
                        deptId:dept_id,
                    },
                    timeout: 3000,
                    dataType: 'json',
                    success:function (res) {
                        if(res.errcode==0){
                            self.all_links= res;
                        }else{
                            self.all_links=[]
                        }
                        <%--搜索科室--%>
                        setTimeout (show,0);
                        function show(){
                            var dept_name = document.querySelectorAll(".dept_name");
                            var search = document.getElementById("search");
                            search.oninput = function() {
                                var value = search.value;
                                [].forEach.call(dept_name, function(item) {
                                    item.style.display = "none";
                                });
                                [].forEach.call(dept_name, function(item) {
                                    var dataName = item.getAttribute("data-name");
                                    if (dataName.indexOf(value) > -1) {
                                        item.style.display = "block";
                                    }
                                });
                            };
                        }
                    },
                    error:function (error) {
                        swal('获取数据失败')
                    }
                });
            },
            <%--选择input与上面显示的关联挂号科室同步--%>
            choose_link:function(all_link){
                var self = this;
                var deptName = all_link.dept_name;
                var dept_id = all_link.dept_id;
                var id = all_link.id;
                var choose_link = {deptName:deptName,deptCode:dept_id,id:id};
                var links = self.all_links.links;
                if(all_link.is_linked == true){
                    links.push(choose_link);
                }else{
                    for(var i=0;i<links.length;i++){
                        if(links[i].deptCode == dept_id){
                            links.splice(i,1);
                        }
                    }
                }
            },
            <%--上面显示的关联挂号科室与选择input同步--%>
            delete_choose_link:function(deptCode){
                var self = this;
                var links = self.all_links.links;
                var all_link = self.all_links.depts;
                for(var i=0;i<links.length;i++){
                    if(links[i].deptCode == deptCode){
                        links.splice(i,1);
                    }
                }
                for(var i=0;i<all_link.length;i++){
                    if(all_link[i].dept_id == deptCode){
                        all_link[i].is_linked=0
                    }
                }
            },
            link_sure:function(){
                var self = this;
                var all_link = self.all_links.depts;
                var json;
                var dept_id;
                var array = new Array();
                if(all_link.length>0){
                    for(var i=0;i<all_link.length;i++){
                        if(all_link[i].is_linked==1){
                            array.push(all_link[i].dept_id); }
                    }
                    json=JSON.stringify(array);
                    dept_id =self.dept_detail.id;
                    $.ajax({
                        type: 'post',
                        url: "see.jsp",
                        data: {
                            act:"dept_link",
                            dept_id:dept_id,
                            other_str:json,
                        },
                        timeout: 3000,
                        dataType: 'json',
                        success:function (data) {
                            <%--window.location.reload();--%>
                            <%--$('.modal').modal('hide');--%>
                        },
                        error:function (error) {
                            alert('添加失败');
                        }
                    });
                }else{
                    swal('关联科室不能为空')
                }
            },
            link_news:function(dept_id,dept_name){
                if(dept_id){
                    locationHerf("news",{
                        deptCode:dept_id,
                        deptName:dept_name,
                    });
                }
            }
        }
    }
    new Vue({
        el: "#app",
        components: {
            dept: dept
        }
    });


</script>
